
local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)

function MainScene:ctor()
    cc.ui.UILabel.new({
            UILabelType = 2, text = "Hello, World", size = 64})
        :align(display.CENTER, display.cx, display.cy*3/2)
        :addTo(self)

    print('scene size : w=%g,h=%g',display.width,display.height)
    local wLabel = string.format('display.width=%g',display.width)
    local hLabel = string.format('display.height=%g',display.height)
    local label = wLabel .. '    ' .. hLabel
    cc.ui.UILabel.new({
            UILabelType = 2, text = label, size = 48})
        :align(display.CENTER, display.cx, display.cy)
        :addTo(self)

    wLabel = string.format('display.sizeInPixels.width=%g',display.sizeInPixels.width)
    hLabel = string.format('display.sizeInPixels.height=%g',display.sizeInPixels.height)
    label = wLabel .. '    ' .. hLabel
    cc.ui.UILabel.new({
            UILabelType = 2, text = label, size = 24})
        :align(display.CENTER, display.cx, display.cy/2)
        :addTo(self)

    local layer = display.newLayer():addTo(self)
    display.newSolidCircle(20, {x=10,y=10,color=cc.c4f(1.0,1.0,0,1.0)}):addTo(layer)
    display.newSolidCircle(20, {x=display.width-10,y=display.height-10,color=cc.c4f(0.0,1.0,1.0,1.0)}):addTo(layer)
    display.newRect(cc.rect(-10,display.height-10,20,20), {fillColor=cc.c4f(0.0,1.0,1.1,1.0), borderColor = cc.c4f(0,1,0,1), borderWidth = 2}):addTo(layer)
    display.newRect(cc.rect(display.width-10,-10,20,20), {fillColor=cc.c4f(0.0,1.0,1.1,1.0), borderColor = cc.c4f(0,1,0,1), borderWidth = 2}):addTo(layer)
    display.newRect(cc.rect(display.cx,display.cy,20,20), {fillColor=cc.c4f(0.0,1.0,1.1,1.0),borderColor = cc.c4f(0,1,0,1), borderWidth = 2}):addTo(layer)
    display.newPolygon({
    	{0,0},
    	{0,display.height},
    	{display.width,display.height},
    	{display.width,0}
    	}, 
    	{borderColor = cc.c4f(1,0,0,1),
    	borderWidth = 3}
    ):addTo(layer)

    local touch = display.newLayer():addTo(self)
    touch:setTouchEnabled(true)
    touch:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
    	if event.name == 'began' then
    		print('touch layer on began!')
    	elseif event.name == 'moved' then
    		--print('touch layer on moved!')
    		local dx = event.x - event.prevX
    		local dy = event.y - event.prevY
    		--print('dx dy = ',dx,dy)
    		local x = layer:getPositionX() + dx
    		local y = layer:getPositionY() + dy
    		layer:pos(x, y)
    	end
    	return true
    end)

    local sprite = display.newLayer():addTo(self)
    display.newRect(cc.rect(display.cx-20,display.cy-20,40,40), {fillColor=cc.c4f(0.2,0.5,1.0,1.0),borderColor = cc.c4f(1,1,1,1), borderWidth = 2}):addTo(sprite)
    sprite:setTouchEnabled(true)
    sprite:setTouchSwallowEnabled(false)
    sprite:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
    	if event.name == 'began' then
    		print('sprite layer on began!',event.x,event.y)
    		if (event.x < display.cx-20) or (event.x > display.cx+20) then return true
    		elseif (event.y < display.cy-20) or (event.y > display.cy+20) then return true
    		else 
    			layer:pos(0,0)
    			return false
    		end
    	else
    		return true
    	end
    end)

    --local background = display.newSprite('background.png'):addTo(layer)
    --local background = display.newSprite('background2.png'):addTo(layer)
    local background = display.newSprite('background3.png'):addTo(layer)
    background:pos(background:getContentSize().width/2,background:getContentSize().height/2)
    --background:setScale(0.5)
    background:setOpacity(128)

    local rotation = display.newSprite():addTo(layer)
    rotation:pos(display.cx,display.cy)
    display.newSolidCircle(20, {x=0,y=60,color=cc.c4f(1.0,0.0,0,1.0)}):addTo(rotation)
    local rot = cc.RotateBy:create(1.0, 180)
    local fer = cc.RepeatForever:create(rot)
    rotation:runAction(fer)

end

function MainScene:onEnter()
end

function MainScene:onExit()
end

return MainScene
